package com.example.zhaoy.rebook;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zhaoy.rebook.dao.Book;
import com.example.zhaoy.rebook.net.DouBanService;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.CaptureActivity;

public class MainActivity extends CaptureActivity {
    private IntentIntegrator integrator;
    private String ISBN;
    private String str;
    EditText edt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button)findViewById(R.id.button);
        edt=(EditText)findViewById(R.id.ISBN);


        integrator = new IntentIntegrator(this);
        integrator.setPrompt("请扫描"); //底部的提示文字，移动开发，设为""可以置空
        integrator.setCameraId(0); //前置或者后置摄像头
        integrator.setBeepEnabled(true); //扫描成功的「哔哔」声，默认开启
        integrator.setCaptureActivity(ScanActivity.class);
        integrator.setOrientationLocked(false);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    public void run(){
                        try {
                           Book book=new DouBanService("https://api.douban.com/v2/book/isbn/9787308083256").serachBook();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
//                integrator.initiateScan();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (result != null) {
            if (result.getContents() == null) {
                Log.d("Tag", "Cancelled");
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
            } else {
                Log.d("Tag", "Scanned: " + result.getContents());

                ISBN=result.getContents();
                str="https://api.douban.com/v2/book/isbn/";
                str=str+ISBN;
                new Thread(){
                    public void run(){
                        try {
                            Book book=new DouBanService(str).serachBook();
                            edt.setText(book.getTitle());

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        }

    }
}
